package com.lsk1998.administrative_division;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    private SQLiteDatabase db;
    ArrayList<Province> list1=new ArrayList<>();
    ArrayList<City> list2=new ArrayList<>();
    ArrayList<County> list3=new ArrayList<>();
    ArrayAdapter<String> adapter1;
    ArrayAdapter<String> adapter2;
    ArrayAdapter<String> adapter3;
    ListView listView1;
    ListView listView2;
    ListView listView3;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化，只需要调用一次
        AssetsDatabaseManager.initManager(getApplication());
        // 获取管理对象，因为数据库需要通过管理对象才能够获取
        AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();
        db = mg.getDatabase("datas.db");

        listView1=findViewById(R.id.list1);
        listView2=findViewById(R.id.list2);
        listView3=findViewById(R.id.list3);
        findProvince();
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //33 32 31
                if(position==31)
                {
                    Toast.makeText(MainActivity.this,"台湾省数据暂缺",Toast.LENGTH_LONG).show();
                }
                else if (position==32)
                {
                    Toast.makeText(MainActivity.this,"香港特别行政区数据暂缺",Toast.LENGTH_LONG).show();
                }
                else if(position==33)
                {
                    Toast.makeText(MainActivity.this,"澳门特别行政区数据暂缺",Toast.LENGTH_LONG).show();
                }
                else
                    findCity(list1.get(position).getPro_id());
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                findCounty(list2.get(position).getCity_id());
            }
        });
        listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                County county = list3.get(position);
                //findAll(county.getPro_id(),county.getCity_id(),county.getCou_id());
                findAll(county.getCou_id());

            }
        });
    }
    //查询省
    public void findProvince()
    {
        if(list2!=null)
            list2.clear();
        if(list3!=null)
            list3.clear();
        Cursor cursor = db.rawQuery("SELECT * FROM province", new String[]{});
        while(cursor.moveToNext())
        {
            list1.add(new Province(cursor.getInt(cursor.getColumnIndex("pro_id")),
                    cursor.getString(cursor.getColumnIndex("name"))));
        }
        final ArrayList<String> list=new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            list.add(list1.get(i).getName());
        }
        Log.i("MainActivity",list1.toString());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter1=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
                listView1.setAdapter(adapter1);
                if(adapter2!=null)
                    adapter2.notifyDataSetChanged();
                if(adapter3!=null)
                    adapter3.notifyDataSetChanged();

                //adapter.notifyDataSetChanged();
                //adapter.notifyDataSetChanged();
            }
        });



    }
    //查询市
    public void findCity(int pro_id)
    {
        list2.clear();
       if(list3!=null)
        list3.clear();
        /*
        SELECT name FROM city WHERE city.pro_id=610000;
       */
        Cursor cursor = db.rawQuery("SELECT * FROM city WHERE city.pro_id=?", new String[]{String.valueOf(pro_id)});
        while(cursor.moveToNext())
        {
            City city = new City();
            city.setCity_id(cursor.getInt(cursor.getColumnIndex("city_id")));
            city.setPro_id(cursor.getInt(cursor.getColumnIndex("pro_id")));
            city.setName(cursor.getString(cursor.getColumnIndex("name")));
            list2.add(city);
        }
        final ArrayList<String> list=new ArrayList<>();
        if(pro_id==710000)
        {
            //台湾省
            list.add("台湾省数据暂缺");
        }
        else if(pro_id==810000)
        {
            list.add("香港特别行政区");
        }
        else if(pro_id==820000)
        {
            list.add("澳门特别行政区");
        }
        else
        {
            for (int i = 0; i < list2.size(); i++) {
                list.add(list2.get(i).getName());
            }
        }


        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter2=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
                listView2.setAdapter(adapter2);
                 adapter2.notifyDataSetChanged();
                 if(adapter3!=null)
                  adapter3=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,new String[]{});
                listView3.setAdapter(adapter3);
                 //adapter3.notifyDataSetChanged();
                //adapter.notifyDataSetChanged();
            }
        });

    }
    //查询县
    public void findCounty(int city_id)
    {
        /*
        SELECT
       name
       FROM
       county
        WHERE
       county.city_id=610300;
        */
        list3.clear();
        Cursor cursor = db.rawQuery("SELECT * FROM county WHERE county.city_id=?",
                new String[]{String.valueOf(city_id)});
        while (cursor.moveToNext())
        {
            County county=new County();
            county.setCity_id(cursor.getInt(cursor.getColumnIndex("city_id")));
            county.setPro_id(cursor.getInt(cursor.getColumnIndex("pro_id")));
            county.setCou_id(cursor.getInt(cursor.getColumnIndex("cou_id")));
            county.setName(cursor.getString(cursor.getColumnIndex("name")));
            list3.add(county);
        }
        final ArrayList<String> list=new ArrayList<>();

        for (int i = 0; i < list3.size(); i++) {
            list.add(list3.get(i).getName());
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter3=new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item,list);
                listView3.setAdapter(adapter3);
                adapter3.notifyDataSetChanged();
                //adapter.notifyDataSetChanged();
            }
        });


    }
    //查询详情
    public void findAll(int cou_id)
    {
        /*
        SELECT province.name AS pro,
       city.name AS city,
       county.name AS cou
        FROM province,
       city,
       county
        WHERE province.pro_id = county.pro_id AND
       city.city_id = county.city_id AND
       county.cou_id = 610304;
        */
        String sql="SELECT province.name AS pro,\n" +
                "       city.name AS city,\n" +
                "       county.name AS cou\n" +
                "        FROM province,\n" +
                "       city,\n" +
                "       county\n" +
                "        WHERE province.pro_id = county.pro_id AND\n" +
                "       city.city_id = county.city_id AND\n" +
                "       county.cou_id = ?;";
        Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(cou_id)});
        StringBuilder builder=new StringBuilder();
        if(cursor.moveToNext())
        {
            builder.append(cursor.getString(cursor.getColumnIndex("pro")));
            builder.append(cursor.getString(cursor.getColumnIndex("city")));
            builder.append(cursor.getString(cursor.getColumnIndex("cou")));

        }

        Toast.makeText(this,builder.toString(),Toast.LENGTH_LONG).show();


    }

}
